<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>信息</title>
		<script src="js/jquery.min.js"></script>
	</head>
	<body>
		<h2>请输入信息...</h2>
		<form id="form1">
			<table>
				<tr>
					<td>用户名</td>
					<input id="ID" name="ID" type="text" value="" style="display: none;" />
					<td><input name="dName" type="text" value="" /></td>
				</tr>
				<tr>
					<td>年龄</td>
					<td><input name="dAge" type="text" value="" /></td>
				</tr>
				<tr>
					<td>性别</td>
					<td><input type="radio" name="dSex" value="男">男
						<input type="radio" name="dSex" value="女">女
					</td>
				</tr>
				<tr>
					<td>密码</td>
					<td><input name="dPwd" type="password" value="" /></td>
				</tr>
				<tr>
					<td>是否为管理员</td>
					<td><input type="radio" name="dAdmin" value="true">是
						<input type="radio" name="dAdmin" value="false">否
				</tr>
				<tr>
					<td>权限1</td>
					<td><input type="radio" name="edit_top" value="y">是
						<input type="radio" name="edit_top" value="n">否
				</tr>
				<tr>
					<td>权限2</td>
					<td><input type="radio" name="edit_middle" value="y">是
						<input type="radio" name="edit_middle" value="n">否
				</tr>
				<tr>
					<td>权限3</td>
					<td><input type="radio" name="edit_below" value="y">是
						<input type="radio" name="edit_below" value="n">否
				</tr>
			</table>
			<input type="button" onclick="sub()" value="提交" />
		</form>
	</body>
	<script type="text/javascript">
		$(function() {
			//新增or更新
			var afterUrl = window.location.search.substring(1);
			console.log(afterUrl);
			if (afterUrl) {
				//回显ID
				var array = afterUrl.split("=");
				var data = array[1];
				$("#ID").val(data);
				//回显用户信息
				$.ajax({
					url: "http://localhost:8080/admin/selectUserAll?ID=" + data,
					success: function(result) {
						if (result.status == 200) {
							let data = result.data[0];
							console.log(data);
							$("input[name='dName']").val(data.dname);
							$("input[name='dAge']").val(data.dage);
							$("input[name='dSex'][value=" + data.dsex + "]").prop("checked", "checked");
							$("input[name='dPwd']").val(data.dpwd);
							$("input[name='dAdmin'][value=" + data.dadmin + "]").prop("checked", "checked");
							$("input[name='edit_top'][value=" + data.edit_top + "]").prop("checked", "checked");
							$("input[name='edit_middle'][value=" + data.edit_middle + "]").prop("checked", "checked");
							$("input[name='edit_below'][value=" + data.edit_below + "]").prop("checked", "checked");
							alert("更新");
						}
					}
				});
			} else {
				alert("新增用户")
			}

		})

		function sub() {
			var id=$("#ID").val();
			console.log(id);
			let form = $("#form1");
			let FormData = form.serializeArray();
			console.log(FormData);
			let json1 = {};
			for (let i = 0; i < FormData.length; i++) {
				json1[FormData[i].name] = FormData[i].value;
			}
			console.log(json1);
			if (id != "") {
				//更新
				$.ajax({
					url: "http://localhost:8080/admin/update?ID="+id,
					data: json1,
					success: function(result) {
						if (result.status == 200) {
							alert(result.msg)
							// window.location.replace("http://localhost:8080/demo/admin.html")
							self.opener.location.reload();
							window.close();
						}
					}
				});
			} else{
				//新增
				$.ajax({
					url: "http://localhost:8080/admin/add",
					data: json1,
					success: function(result) {
						if (result.status == 200) {
							alert(result.msg)
							// window.location.replace("http://localhost:8080/demo/admin.html")
							self.opener.location.reload();
							window.close();
						}
					}
				});
			}
		}

	</script>
</html>
